package com.code.springbootmybatismysql.configuration.datasource;

import org.springframework.util.Assert;

/**
 * 使用ThreadLocal技术来记录当前线程中的数据源的key
 */
public class DbContextHolder {
    private static final ThreadLocal<String> sContextHolder = new ThreadLocal();

    public static void setDataSource(String dataSource) {
        Assert.notNull(dataSource, "dataSource can't be null");
        sContextHolder.set(dataSource);
    }

    public static String getDataSource() {
        return sContextHolder.get();
    }

    public static void clearDataSource() {
        sContextHolder.remove();
    }
}
